package cn.btks.resultintegration.catalog.repository;

import cn.btks.resultintegration.catalog.pojo.Catalog;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author lijun
 * @Date 2020/12/14 17:16
 */
@Repository
public interface CatalogRepository extends JpaRepository<Catalog,Long> {

    /**
     * 根据编码code查询目录
     * @param code
     * @return
     */
    Catalog findByCode(String code);

    /**
     * 根据keycode查询目录
     * @param keyCode
     * @return
     */
    Catalog findByKeyCode(String keyCode);

    /**
     * 根据keycode查询目录
     * @param keyCode
     * @return
     */
    @Query(value = "select * from catalog where key_code like '%' ?1 '%'",nativeQuery = true)
    Catalog findLikeKeyCode(String keyCode);

    /**
     * 根据上级目录ID查询目录
     * @param upLevelId
     * @return
     */
    List<Catalog> findAllByUpLevelId(Long upLevelId);

    /**
     * 根据等级及上级等级ID获取目录对象
     * @param level
     * @param upLevelId
     * @param catalogOrder
     * @return
     */
    Catalog findByLevelAndUpLevelIdAndCatalogOrder(Integer level,Long upLevelId,Long catalogOrder);

    /**
     * 根据等级获取该等级下的最大值
     * @param level
     * @param upLevelId
     * @return
     */
    @Query(value = "SELECT MAX(catalog_order) FROM catalog WHERE level = ?1 AND up_level_id = ?2",nativeQuery = true)
    Integer findByMaxOrder(Integer level,Long upLevelId);

    /**
     * 根据ID删除目录信息
     * @param id
     */
    @Modifying
    @Query(value = "delete from catalog where identity = ?1",nativeQuery = true)
    void deleteById(Long id);

    /**
     * 根据code模糊删除目录信息
     * @param code
     */
    @Modifying
    @Query(value = "delete from catalog where code like ?1 '%'",nativeQuery = true)
    int deleteByCode(String code);
}
